package userclass;

import java.time.format.DateTimeFormatter;
import java.io.PrintWriter;
import java.io.Serializable;
import java.time.*;
import java.util.Date;
import java.util.Formatter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Map.Entry;
import java.util.TreeMap;


import libraryClass.Book;
import libraryClass.Library;
import libraryClass.Library.LendOrBack;

public class Student extends User implements Serializable{
	public class LoanInf implements Serializable{
//		String Date=DateFormat.getDateTimeInstance().format(new java.util.Date());

//		Date date = new Date();
//		String Date = String.format("%tF", date);
//		String DeadLine = String.format("%tF", new Date(date.getTime()+(long)30*24*60*60*1000));

		LocalDate date=LocalDate.now();
		LocalDate deadLineDate=date.plusMonths(1);

		public String getDate() {
			return date.format(DateTimeFormatter.ofPattern("E MM/dd/yyyy"));

		}

		public String getDeadLine() {
			return deadLineDate.format(DateTimeFormatter.ofPattern("E MM/dd/yyyy"));

		}


	}

	private Map<Book,LoanInf>  bookLoan=new TreeMap<Book,LoanInf>();

	public Map<Book, LoanInf> getBookLoan() {
		return bookLoan;
	}


	public Student(String name, long iD, String password) {
		super(name, iD, password);
		// TODO 自动生成的构造函数存根
	}
	public void displayloan(PrintWriter out) {
		Iterator<Map.Entry<Book, LoanInf>> m =bookLoan.entrySet().iterator();

		out.println("     书名\t\t\t"+"ID\t\t"+"借阅日期"+"\t\t归还日期");
		while(m.hasNext()){
			Entry<Book, LoanInf> e = m.next();
			out.println("《"+e.getKey().getName()+"》  \t\t"+e.getKey().getID()+"\t\t"+e.getValue().date+"\t"+e.getValue().deadLineDate);
		}
	}

	public Book peekBook(long ID) {
		Iterator<Map.Entry<Book, LoanInf>> m = bookLoan.entrySet().iterator();
		while(m.hasNext()){
			Map.Entry<Book, LoanInf> e = m.next();
			if(e.getKey().getID()==ID)
				return e.getKey();
		}
		return null;
	}
	public ArrayList<Book> peekBooks(String name) {
		ArrayList<Book> bookList=new ArrayList<Book> ();
		Iterator<Map.Entry<Book, LoanInf>> m = bookLoan.entrySet().iterator();
		while(m.hasNext()){
			Map.Entry<Book, LoanInf> e = m.next();
			if(e.getKey().getName()==name)
				bookList.add(e.getKey());

		}
		return bookList;
	}
//	Book borrow( String name ){
//		ArrayList<Book> bookList=Library.peekBook(name);
//		if(bookList.isEmpty()) {
//			System.out.println("该书籍不存在，请重新输入书名或ID");
//			return null;
//		}
//		System.out.println("搜索到一下相关书籍：");
//		System.out.println("书名\t\t"+"ID\t\t"+"类别");
//		for(Book b:bookList)
//			System.out.println(b);
//		
//		LoanInf inf=new LoanInf();
//		bookLoan.put(book,inf );
//		
//		Library.remove(book);
//		return book;
//	}



	public Book borrow( long ID ) {
		Book book=Library.peekBook(ID);
		if(book==null) {
			return null;
		}
		LoanInf inf=new LoanInf();
		bookLoan.put(book,inf );

		Library.lendOrBack(book, LendOrBack.LEND);
		return book;
	}

	public Book giveback( long ID ) {
		Book book=peekBook(ID);
		if(book==null) {
			return null;
		}

		bookLoan.remove(book);
		Library.lendOrBack(book, LendOrBack.BACK);
		return book;
	}


}
